<?php

namespace Admin\Controller;

use Think\Controller;

class SortController extends CommonController
{

    public function ajaxSort()//分类管理
    {

        $model_sort = D('sort');
        $n = I('get.limit');
        $n = !$n ? 6 : $n;

        $p = I('get.page');
        $p = $p <= 0 ? 1 : $p;

        $p = ($p - 1) * $n;//(页码 - 1 ) * 单页条数

        $res = $model_sort->query("SELECT * FROM sort limit $p,$n");// 从第$p条开始 取$n条

        $count = $model_sort->query("SELECT count(1)  AS num FROM sort ");

        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '暂无信息' : '提示信息';
        $this->ajaxRet($code, $msg, $res, $count[0]['num']);
//        $this->ajaxReturn(array('data' => $res, 'count' => $count[0]['num'], 'code' => empty($res) ? 1 : 0, 'msg' => empty($res) ? '暂无信息' : '提示信息'));

    }

    public function add()//添加分类
    {
        $model_link = D('sort');
///        $post=$_POST;
        if (IS_POST) {
            $data['title'] = I('post.title');
            if ($model_link->create($data)) {
                if ($model_link->add()) {
                    $this->success('添加分类成功！', U('Sort/add'));
                } else {
                    $this->arror('添加分类失败！');
                }
            } else {
                $this->error($model_link->getError());
            }
            return;
        }
        $this->display();
    }

    public function del()//删除
    {
        $data = $_POST;
        $model_del = M("sort"); // 实例化User对象
        $model_del->where("id=$data[id]")->delete(); // 删除id为5的用户数据
        $count = $model_del->query("SELECT count(1)  AS num FROM sort ");
        $this->ajaxRet(self::RET_SUCCESS, '删除成功');
//        $this->ajaxReturn(array('data' => $model_del, 'count' => $count[0]['num'], 'code' => empty($model_up) ? 1 : 0, 'msg' => empty($model_up) ? '暂无信息' : '提示信息'));


    }

    public function up()//修改
    {
        $data = $_POST;
        $model_up = M("sort"); // 实例化User对象
        $count = $model_up->query("SELECT count(1)  AS num FROM sort ");
        $model_up->where("id=$data[id]")->save($data); // 根据条件更新记录
        $this->ajaxRet(self::RET_SUCCESS, '修改成功');
//        $this->ajaxReturn(array('data' => $model_up, 'count' => $count[0]['num'], 'code' => empty($model_up) ? 1 : 0, 'msg' => empty($model_up) ? '暂无信息' : '提示信息'));

    }

    public function checkSort()//验证类名是否存在
    {
        $til = $_POST['title'];
        $model_check_sort = D('sort');
        $sql = sprintf("SELECT * FROM sort WHERE title = '%s'", $til);
        $res = $model_check_sort->query($sql);
        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '可用' : '不可用';
        $this->ajaxRet($code, $msg);
    }
}